/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical.wizard.spray;

import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneLayout;
import javax.swing.table.AbstractTableModel;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.model.ProductionUnit;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningModule;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

public final class SummaryPanel extends JPanel {
    
    private static final String TITLE = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.summary_panel.title"); 
    
    private final JLabel labelHeaderText = new JLabel(NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.summary_panel.header_text"));
 
    private static final int PREFERED_PANE_HEIGHT = 150;
    private static final int PREFERED_PANE_WIDTH = 350;
    
    // Text Labels
    private static final String FIELDPLAN_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.fieldplan");
    private static final String PU_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.production_unit_name");
    private static final String CROP_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.crop");
    private static final String PU_START_DATE_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.production_unit_name_start_date");
    private static final String PU_END_DATE_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.production_unit_name_end_date");     
    private static final String OPERATION_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.operation");  
    private static final String ACTION_PLAN_DATE_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.label.action_planned_date");  
    private static final String ACTION_NAME = NbBundle.getMessage(TacticalPlanningModule.class,"field_plan.wizard.spray.planning_wizard_action.operation_name");
    
    private static final String LABEL_GAP = "gapleft 20";
    
    // Value labels  
    private ProductionUnit productionUnit = Utilities.actionsGlobalContext().lookup(ProductionUnit.class);
    private final JLabel labelFieldplan = new JLabel(Integer.toString(productionUnit.getFieldPlan().getPlanYear())); // fieldplan year
    private final JLabel labelPU = new JLabel(productionUnit.getName()); // Name
    private final JLabel labelCrop = new JLabel(productionUnit.getCrop()); // Crop name
    private final JLabel labelPUStartDate = new JLabel(); // Pu startdate
    private final JLabel labelPUEndDate = new JLabel();
    private final JLabel labelOperation = new JLabel(ACTION_NAME); // Currently we only have spraying operations   
    private final JLabel labelActionPlanDate = new JLabel();

    // Text-only labels
    private static final String SPRAYING_TITLE_LABEL_TEXT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.summary_panel.label.title");      
    private static final String ACTION_PLANNED = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.common.text.action_planned");  
    
    private String[][] selectedCropProtectionTasks;
    private SummaryTableModel summaryTableModel;   
    private JTable summaryTable;
    private JScrollPane pane;    
    
    /** Creates new form SummaryPanel */
    public SummaryPanel() {
        initComponents();
    }

    @Override
    public String getName() {
        return TITLE;
    }

    private void initComponents() {
        setLayout(new MigLayout("wrap 2"));
 
        this.add(labelHeaderText, "span");
        
        this.add(new JLabel(FIELDPLAN_LABEL_TEXT));
        this.add(labelFieldplan, LABEL_GAP);

        
        this.add(new JLabel(PU_LABEL_TEXT));
        this.add(labelPU, LABEL_GAP);
        
        this.add(new JLabel(CROP_LABEL_TEXT));
        this.add(labelCrop, LABEL_GAP);
        
        this.add(new JLabel(PU_START_DATE_LABEL_TEXT));
        this.add(labelPUStartDate, LABEL_GAP);
   
        this.add(new JLabel(PU_END_DATE_LABEL_TEXT));
        this.add(labelPUEndDate, LABEL_GAP);
        
        // The following action will be planned
        this.add(new JLabel(ACTION_PLANNED), "gaptop 30, span"); 
        
        this.add(new JLabel(OPERATION_LABEL_TEXT));
        this.add(labelOperation, LABEL_GAP);
        
        this.add(new JLabel(ACTION_PLAN_DATE_LABEL_TEXT));
        this.add(labelActionPlanDate, LABEL_GAP);
        
        this.add(new JLabel(SPRAYING_TITLE_LABEL_TEXT), "gaptop 20, wrap");

        summaryTableModel = new SummaryTableModel();
        summaryTable = new JTable(summaryTableModel);      
        summaryTable.setRowSelectionAllowed(false);
        summaryTable.setColumnSelectionAllowed(false);
        summaryTable.setEnabled(false);
        
        pane = new JScrollPane();
        pane.setPreferredSize(new Dimension(PREFERED_PANE_WIDTH, PREFERED_PANE_HEIGHT));
        pane.setLayout(new ScrollPaneLayout());
        pane.setViewportView(summaryTable);
        this.add(pane, "span");    
    }

    /**
     * 
     * 
     * @param selectedCropProtections 
     */
    public void setSelectedCropProtectionTasks(String[][] selectedCropProtectionTasks) {
        this.selectedCropProtectionTasks = selectedCropProtectionTasks.clone();
        summaryTableModel.setData(this.selectedCropProtectionTasks);        
        summaryTable.clearSelection();
    }
   
    public String[][] getSelectedCropProtectionTasksProperties() {
        String[][] cropProtectionProperties = new String[summaryTableModel.getRowCount()][summaryTableModel.getColumnCount()];
        for (int i = 0; i < summaryTableModel.getRowCount(); i++) {
            for (int j = 0; j < summaryTableModel.getColumnCount(); j++) {
                cropProtectionProperties[i][j] = (String) summaryTableModel.getValueAt(i, j);
            }
        }
        return cropProtectionProperties;
    }
        
    public JLabel getActionPlanDateLabel() {
        return labelActionPlanDate;
    }
    
    public JLabel getOperationLabel() {
        return labelOperation;
    }
    
    public JLabel getProductionUnitEnddateLabel() {
        return labelPUEndDate;
    }
           
    public JLabel getProductionUnitStartdateLabel() {
        return labelPUStartDate;
    }        
    
    public JLabel getFieldplanLabel(){
        return labelFieldplan;
    }
    
    public JLabel getProductionUnitLabel(){
        return labelPU;
    }
            
    public JLabel getCropLabel() {
        return labelCrop;
    }

    /**
     * Custom tableModel. this can be instantiated without data. Use the setData method to display data
     */
    private class SummaryTableModel extends AbstractTableModel {

        private final String PESTICIDE = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.crop_header_pesticide");
        private final String QUANTITY = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.planning_visual_panel_crop_protection_edit_properties.columnheader.quantity");
        private final String CONCENTRATION = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.planning_visual_panel_crop_protection_edit_properties.columnheader.concentration");
        private final String[] COLUMNNAMES = new String[]{PESTICIDE, QUANTITY, CONCENTRATION};
        private Object[][] data = new Object[0][0];

        public SummaryTableModel() {
        }

        public void setData(Object[][] data) {
            this.data = data.clone();
        }

        @Override
        public int getColumnCount() {
            return COLUMNNAMES.length;
        }

        @Override
        public int getRowCount() {
            return data.length;
        }

        @Override
        public String getColumnName(int col) {
            return COLUMNNAMES[col];
        }

        @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
        
        // Table should not be editable
        @Override
        public boolean isCellEditable(int x, int y){
            return false;
        }
        
    }
}
